package com.example.demo.util;

import java.io.*;

/**
 * @author: Scott
 * @Description:
 * @Date: Create in 22:28 2017/12/1
 */
public class ByteUtil {
    public static byte[] getBytes(Object obj) {
        ByteArrayOutputStream bout = null;
        ObjectOutputStream out = null;
        byte[] bytes = null;
        try {
            bout = new ByteArrayOutputStream();
            out = new ObjectOutputStream(bout);
            out.writeObject(obj);
            out.flush();
            bytes = bout.toByteArray();
            bout.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


        return bytes;
    }

    public static Object getObject(byte[] bytes) throws IOException, ClassNotFoundException {
        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
        ObjectInputStream oi = new ObjectInputStream(bi);
        Object obj = oi.readObject();
        bi.close();
        oi.close();
        return obj;
    }
}
